package com.android.lovingpet.signin.view;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.home.view.HomeActivity;
import com.android.lovingpet.signin.ISignInContract;
import com.android.lovingpet.signin.SignInPresenter;
import com.android.lovingpet.util.Utility;
import com.github.ybq.android.spinkit.style.DoubleBounce;


/**
 * Created by ikata on 2018/1/20.
 */

public class Sign_in_Contentfragment extends BaseFragment<ISignInContract.View,SignInPresenter> implements View.OnClickListener, ISignInContract.View {

    private EditText et_phone;
    private EditText et_password;
    private ImageView im_password;
    private CheckBox cbTeam;
    private boolean isShow = false;

    @Override
    protected int setContentView() {
        return R.layout.fragment_sign_in_content;
    }

    @Override
    protected void onCreateView(View view) {
        ((TextView) view.findViewById(R.id.tv_title)).setText("登录");
        Button bt_sign_in=view.findViewById(R.id.sign_in);
        et_password=view.findViewById(R.id.sign_in_password);
        et_phone=view.findViewById(R.id.sign_in_phone);
        im_password=view.findViewById(R.id.sign_in_show_password);
        cbTeam = view.findViewById(R.id.cb_team);
        bt_sign_in.setOnClickListener(this);
        et_password.setOnClickListener(this);
        et_phone.setOnClickListener(this);
        im_password.setOnClickListener(this);
    }

    @Override
    public SignInPresenter createPresenter() {
        return new SignInPresenter();
    }
    @Override
    public ISignInContract.View createView() {
        return this;
    }

    @Override
    public void next() {
        Intent intent=new Intent(getActivity(),HomeActivity.class);
        startActivity(intent);
        Utility.exitActivity(getActivity());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in:
                String phone = et_phone.getText().toString();
                String password = et_password.getText().toString();
                getPresenter().sign_in(phone, password, cbTeam.isChecked() ? 0 : 1);
                break;

            case R.id.sign_in_show_password:
                //密码显示
                displayPassword();
                break;
        }
    }

    private void displayPassword() {
        if(isShow) {
            et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
            et_password.setSelection(et_password.getText().length());
            im_password.setBackground(getContext().getResources().getDrawable(R.drawable.image_showpassword));
            isShow = false;
        }
        else {
            et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            et_password.setSelection(et_password.getText().length());
            im_password.setBackground(getContext().getResources().getDrawable(R.drawable.image_noshowpassword));
            isShow = true;
        }
    }
}
